//
//  DiaryViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/15.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "DiaryViewController.h"
#import "DiaryBannerCell.h"
#import "DiaryOldTimeCell.h"
#import "DiaryListCell.h"
#import "DiaryActivityModel.h"
#import "DiaryOldTimeModel.h"
@interface DiaryViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *activityArray;//本期活动数组
@property (nonatomic, strong) NSMutableArray *datasource;//往期回顾数据

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *commendLabel;//评论
@property (nonatomic, strong) UIImageView *aImage;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) LoadingView *load;
@end

@implementation DiaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"日记";
    [self setupView];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(DiaryDoRefresh)];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(DiarydoAppend)];
    _tableView.mj_footer.hidden = YES;
    [_tableView.mj_header beginRefreshing];
    _page = 1;
    
    _load = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT - 64)];
    _load.hidden = YES;
    [_load setReminderLabelTitle:@"没有更多商品" andImage:@"noNetweok"];
    [_load.refreshBtn addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventTouchUpInside];
    _load.refreshBtn.hidden = NO;
    [self.view addSubview:_load];
    if([GW_Request sharedInstance].noNetwork)
    {
        _load.hidden = NO;
    }
}

- (void)handleRefresh:(UIButton *)sender
{
    [self DiaryDoRefresh];
}
- (void)DiaryDoRefresh
{
    _page = 1;
    [self startNetwork];//本期回顾数据请求
    [self getLastActivityData];//往期回顾的数据请求
}

- (void)DiarydoAppend
{
    [self getLastActivityData];
}

- (void)startNetwork
{
    _activityArray = [NSMutableArray arrayWithCapacity:0];
    [_activityArray removeAllObjects];
    //本期活动
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/diary/GetNewlestActivity", GW_Domain] parameters:nil completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"日记-本期活动 %@", resultDict);
        NSDictionary *dict = resultDict[@"data"];
        NSDictionary *left = dict[@"left"];
        NSDictionary *mid = dict[@"mid"];
        NSDictionary *right = dict[@"right"];
        
        DiaryActivityModel *model = [GW_Tool getObjFromDictionary:left byClassName:@"DiaryActivityModel"];
        DiaryActivityModel *model1 = [GW_Tool getObjFromDictionary:mid byClassName:@"DiaryActivityModel"];
        DiaryActivityModel *model2 = [GW_Tool getObjFromDictionary:right byClassName:@"DiaryActivityModel"];

        [_activityArray addObject:model];
        [_activityArray addObject:model1];
        [_activityArray addObject:model2];
        
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"日记错误%@", error);
        [_tableView.mj_header endRefreshing];
    }];
}


- (NSMutableArray *)datasource
{
    if(!_datasource)
    {
        _datasource = [NSMutableArray arrayWithCapacity:0];
    }
    return _datasource;
}

- (void)getLastActivityData
{
   
    @weakobj(self);
    NSDictionary *param = @{@"page":@(_page), @"num":@(kPageSize)};
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/diary/GetLastActivity?page=%ld&num=%ld", GW_Domain, _page, kPageSize] parameters:param
    completionBlockWithSuccess:^(NSDictionary *resultDict) {
        @strongobj(self);
        NSLog(@"日记-往期回顾%@", resultDict);
        if(_page == 1)
        {
            [_datasource removeAllObjects];
        }
        NSArray *arr = resultDict[@"data"];
        for (NSDictionary *dict in resultDict[@"data"]) {
            DiaryOldTimeModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"DiaryOldTimeModel"];
            [self.datasource addObject:model];
        }
        self.page++;
      
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
        
        if (arr.count < kPageSize) {
            [UIView animateWithDuration:0.3 animations:^{
                self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
            }];
        } else {
            self.tableView.mj_footer.hidden = NO;
        }
        
        if(self.datasource.count > 0)
            self.load.hidden = YES;
        else
            self.load.hidden = NO;
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
//        self.load.hidden = NO;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)setupView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT - 64 - 49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[DiaryBannerCell class] forCellReuseIdentifier:@"banner"];
    [_tableView registerClass:[DiaryOldTimeCell class] forCellReuseIdentifier:@"oldtime"];
    [_tableView registerClass:[DiaryListCell class] forCellReuseIdentifier:@"list"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark - UITableviewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2 + self.datasource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row == 0)
        return 290;
    else if(indexPath.row == 1)
       return 70;
    else
        return 200;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        DiaryBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"banner"];
        cell.array = [self.activityArray copy];
        [cell configView];
        cell.viewController = self;
        return cell;
    }
    else if (indexPath.row == 1)
    {
        DiaryOldTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oldtime"];
        return cell;
    }
    else
    {
        DiaryOldTimeModel *model = self.datasource[indexPath.row - 2];
        DiaryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"list"];
        [cell configWithModel:model];
        [cell.btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];//点击查看详情
        cell.btn.tag = indexPath.row - 2 + 2000;
        return cell;
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row > 1)
    {
        DiaryOldTimeModel *model = self.datasource[indexPath.row - 2]; //model.id
        [GW_webView handleWebView:[NSString stringWithFormat:@"%@/diary/rijiDP.html?gid=%@", GW_Domain, model.id] viewController:self titleName:@"日记" goodid:model.id];
    }
}

//点击查看详情
- (void)handleBtnAction:(UIButton *)sender
{
    DiaryOldTimeModel *model = self.datasource[sender.tag - 2000];
    [GW_webView handleWebView:[NSString stringWithFormat:@"%@/diary/rijiDP.html?gid=%@", GW_Domain,model.id] viewController:self titleName:@"日记" goodid:model.id];
}


- (void)handleTapAction:(UITapGestureRecognizer *)tap
{
    
    if(tap.view.tag == 1001)
    {
        DiaryActivityModel *model = self.activityArray[0];
        [GW_webView handleWebView:[NSString stringWithFormat:@"%@/diary/rijiDP.html?gid=%@", GW_Domain,model.id] viewController:self titleName:nil goodid:model.id];
    }
    else if(tap.view.tag == 1002)
    {
        DiaryActivityModel *model = self.activityArray[1];
        [GW_webView handleWebView:[NSString stringWithFormat:@"%@/diary/rijiDP.html?gid=%@",  GW_Domain,model.id] viewController:self titleName:nil goodid:model.id];
    }
    else
    {
        DiaryActivityModel *model = self.activityArray[2];
        [GW_webView handleWebView:[NSString stringWithFormat:@"%@/diary/rijiDP.html?gid=%@", GW_Domain,model.id] viewController:self titleName:nil goodid:model.id];
    }
        
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
//    [[UINavigationBar appearance] setTranslucent:NO];//设置导航条不透明
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
